package com.tencent.wxcloudrun.utils;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * @author lichongbing
 * @version 1.0.0
 * @createdate 2022/4/23 3:25 下午
 * @description: TODO
 */
public class DateUtils {

    /**
     * @author lichongbing
     * @update 2022/2/15 1:56 下午
     * @description: 新增计时器
     */
    private static SimpleDateFormat sf = null;
    public static long getDuration(String oldstart, String start) {
        DateTime oldTime = DateUtil.parse(oldstart);
        DateTime newTime = DateUtil.parse(start);
        long betweenMinutes = DateUtil.between(oldTime, newTime, DateUnit.SECOND);
        return betweenMinutes;
    }

    // 获取现在时间
    public static String getEndTime() {
        String endTime = DateUtil.format(DateUtil.date(), "yyyyMMddHHmmss");
        return endTime;
    }
    public static String getEndTime(String format) {
        String endTime = DateUtil.format(DateUtil.date(), format);
        return endTime;
    }

    //获取昨天晚上10点的时间
    public static String getStartTime() {
        String startTime = DateUtil.format(DateUtil.offsetDay(DateUtil.date(), -2), "yyyy-MM-dd") + " 23:59:59";
        return startTime;
    }

    /**
     * 根据传入时间 获取昨天的开始时间
     *
     * @param time
     * @return
     */
    public static String getLastDayStartDate(String time) {
        SimpleDateFormat foramt = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = foramt.parse(time);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, -1);//当前时间减一天

        return foramt.format(cal.getTime()) + " 00:00:00";
    }

    /**
     * 根据传入时间 获取明天的结束时间
     *
     * @param time
     * @return
     */
    public static String getDayEndDate(String time) {
        SimpleDateFormat foramt = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = foramt.parse(time);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, -1);//当前时间减一天

        return foramt.format(cal.getTime()) + " 23:59:59";
    }


    /**
     * 根据传入时间 获取明天的结束时间
     *
     * @param time
     * @return
     */
    public static String getNextDayEndDate(String time) {
        SimpleDateFormat foramt = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = foramt.parse(time);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, 1);//当前时间减一天

        return foramt.format(cal.getTime()) + " 00:00:00";
    }



    /**
     * 根据传入时间 获取上周一时间
     *
     * @param time
     * @return
     */
    public static String getLastWeekMonday(String time) {
        SimpleDateFormat foramt = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = foramt.parse(time);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        if (1 == cal.get(Calendar.DAY_OF_WEEK)) {//判断当前日期是否为周末，因为周末是本周第一天，如果不向后推迟一天的到的将是下周一的零点，而不是本周周一零点
            cal.add(Calendar.DATE, -1);
        }
        cal.add(Calendar.DAY_OF_MONTH, -7);//时间减去7天
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);//Calendar.MONDAY 这个是周一的意思      想获取周几就更换这个

        return foramt.format(cal.getTime()) + " 00:00:00";
    }




    /**
     * @param
     * @return
     */
    public static String getLastWeekSunday() {

        String time = DateUtil.format(DateUtil.date(), "yyyy-MM-dd");
        SimpleDateFormat foramt = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = foramt.parse(time);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        if (1 == cal.get(Calendar.DAY_OF_WEEK)) {//判断当前日期是否为周末，因为周末是本周第一天，如果不向后推迟一天的到的将是下周一的零点，而不是本周周一零点
            cal.add(Calendar.DATE, 0);
        }


        cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);//Calendar.SUNDAY 周天想获取周几就更换这个
        return foramt.format(cal.getTime()) + " 00:00:00";
    }


    /**
     * @param time
     * @return
     */
    public static String getLastMonthFirstDate(String time) {
        SimpleDateFormat foramt = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        String monthFirstDate = "";
        try {
            date = foramt.parse(time);
            Calendar c = Calendar.getInstance();
            //设置为指定日期
            c.setTime(date);
            //指定日期月份减去一
            c.add(Calendar.MONTH, -1);
            //指定日期月份减去一后的 获取第一天
            c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));
            //获取最终的时间
            Date lastDateOfPrevMonth = c.getTime();
            monthFirstDate = foramt.format(lastDateOfPrevMonth) + " 00:00:00";
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return monthFirstDate;
    }

    /**
     * 根据传入时间 获取上月最后一天一时间
     *
     * @param time
     * @return
     */
    public static String getLastMonthEndDate(String time) {
        SimpleDateFormat foramt = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        String monthFirstDate = "";
        try {
            date = foramt.parse(time);
            Calendar c = Calendar.getInstance();
            //设置为指定日期
            c.setTime(date);
            //指定日期月份减去一
            c.add(Calendar.MONTH, -1);
            //指定日期月份减去一后的 最大天数
            c.set(Calendar.DATE, c.getActualMaximum(Calendar.DATE));
            //获取最终的时间
            Date lastDateOfPrevMonth = c.getTime();
            monthFirstDate = foramt.format(lastDateOfPrevMonth) + " 23:59:59";
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return monthFirstDate;
    }

    //根据传入时间 获取本周星期六的结束时间
    public static String getWeekEndDate() {

        String time = DateUtil.format(DateUtil.date(), "yyyy-MM-dd");
        SimpleDateFormat foramt = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = foramt.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        if (7 == cal.get(Calendar.DAY_OF_WEEK)) {
            cal.add(Calendar.DATE, 0);
        } else if (6 == cal.get(Calendar.DAY_OF_WEEK)) {
            cal.add(Calendar.DATE, 1);
        } else if (5 == cal.get(Calendar.DAY_OF_WEEK)) {
            cal.add(Calendar.DATE, 2);
        } else if (4 == cal.get(Calendar.DAY_OF_WEEK)) {
            cal.add(Calendar.DATE, 3);
        } else if (3 == cal.get(Calendar.DAY_OF_WEEK)) {
            cal.add(Calendar.DATE, 4);
        } else if (2 == cal.get(Calendar.DAY_OF_WEEK)) {
            cal.add(Calendar.DATE, 5);
        } else if (1 == cal.get(Calendar.DAY_OF_WEEK)) {
            cal.add(Calendar.DATE, 6);
        }

        return foramt.format(cal.getTime()) + " 23:59:59";
    }


    //转换时间字符串格式
    public static String getDateStr(String dateStr) {
    	String date = "";
    	try {
    		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    		date = sdf.format(sdf.parse(dateStr));
    	} catch (ParseException e) {
    		e.printStackTrace();
    	}
    	return date;
    }

      //EEE, dd MMM yyyy HH:mm:ss Z
     // 把"Mon, 25 Apr 2022 09:02:51 +0800" 转换成 "2022-4-25 09:02:51"
    public static String getDateStEnlishToLocal(String dateStr) {
        sf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
        Date date = new Date();
        try {
            date = sf.parse(dateStr);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        Date date1 = new Date(date.getTime());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(date1);
       // return date.toString();
    }

    //将字符串转换为时间戳
    public static String getStringToDate(String time) {
        sf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
        Date date = new Date();
        try {
            date = sf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Date date1 = new Date(date.getTime());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(date1);
    }

    public static long getStringToLongDate(String time) {
        sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        try {
            date = sf.parse(time);
            long time1 = date.getTime();
            return time1;
        } catch (ParseException e) {
            e.printStackTrace();
            long timestamp = System.currentTimeMillis();
            return timestamp;
        }



    }



}
